Special cases in degree calculations

  • A self-loop in an undirected graph contributes +2 to the vertex's degree because it provides two "ends" at the same vertex. In directed graphs, a self-loop adds +1 to both in-degree and out-degree, since the edge both leaves and enters the vertex.
  • Parallel edges (multiple edges between the same pair of vertices) each contribute to the degree count. If there are k parallel edges between vertices u and v, they add k to deg(u) and k to deg(v) in undirected graphs, requiring containers like Counter instead of sets.
  • Standard set-based adjacency lists cannot represent multigraphs because sets automatically deduplicate entries. To handle parallel edges, use lists or Counter objects that preserve multiplicity, tracking how many edges exist between each pair of vertices.
  • When computing degrees in graphs with loops and parallel edges, you must explicitly account for these special cases. The simple formula deg(v) = |adj[v]| only works for simple graphs; multigraphs require summing edge multiplicities, and loops need special handling in the counting logic.